<%@ page language="java" import="weaver.conn.RecordSet" pageEncoding="UTF-8" %>
<%@page import="weaver.general.Util" %>
<%@page import="weaver.hrm.HrmUserVarify" %>
<%@page import="weaver.hrm.User" %>
<%@page import="java.text.ParseException" %>
<%@page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.*" %>
<jsp:useBean id="SubCompanyComInfo" class="weaver.hrm.company.SubCompanyComInfo" scope="page"/>
<jsp:useBean id="DepartmentComInfo" class="weaver.hrm.company.DepartmentComInfo" scope="page"/>
<jsp:useBean id="ResourceComInfo" class="weaver.hrm.resource.ResourceComInfo" scope="page"/>
<%
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
    RecordSet rs2 = new RecordSet();

    String weekArr[] = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
    String sjd[] = {"上午", "下午", "全天"};
    int[] sjdIs = {0, 1, 2};
    User user = HrmUserVarify.getUser( request, response );
    int subcompanyid1 = user.getUserSubCompany1();
    int userid = user.getUID();

    String formid = Util.null2String( request.getParameter( "formid" ) );
    String tablename = "";
    if (formid.equals( "" )) {
        formid = "-31";//本周重点安排对应的表单的id
    }

    String billid = Util.null2String( request.getParameter( "billid" ) );

    String formsql = "select * from workflow_bill where id=" + formid;
    rs.executeSql( formsql );
    if (rs.next()) {
        tablename = rs.getString( "tablename" );
    }

    String type = Util.null2String( request.getParameter( "type" ) );
    String rq = Util.null2String( request.getParameter( "rq" ) );
    String hdnr = Util.null2String( request.getParameter( "field37274" ) );

    String startdate1 = Util.null2String( request.getParameter( "startdate1" ) );
    String enddate1 = Util.null2String( request.getParameter( "enddate1" ) );
    String from = Util.null2String( request.getParameter( "from" ) );

    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
    Date curdate = new Date();

    if (rq.equals( "" )) {
        rq = sdf.format( curdate );
    }
    if (type.equals( "0" )) {
        startdate1 = "";
        enddate1 = "";

        curdate = sdf.parse( rq );
    }
    Calendar calendar = Calendar.getInstance();
    int week = curdate.getDay();
    if (week == 0) {
        week = 7;
    }
    calendar.setTime( curdate );
    calendar.add( Calendar.DATE, 1 - week );
    String week1 = sdf.format( calendar.getTime() );
    calendar.add( Calendar.DATE, 6 );
    String week7 = sdf.format( calendar.getTime() );

    if (startdate1.equals( "" )) {
        startdate1 = week1;
    }
    if (enddate1.equals( "" )) {
        enddate1 = week7;
    }

    Date _date = sdf.parse( startdate1 );
    calendar.setTime( _date );
    String weekOfYear = calendar.get( Calendar.WEEK_OF_YEAR ) + "";


    Map<String, Object> map = new HashMap<String, Object>();
    //如果没有选时间段

    String sql = "select a.* from " + tablename + " a where (sfktap=1 and ksrq>='" +
            startdate1 + "' and ksrq<='" + enddate1 + "') and hysy like '%" + hdnr + "%'  " +
            "order by ksrq,kssj";
//    out.print(sql+"</br>");

    rs1.executeSql( sql );
    while (rs1.next()) {
        Map<String, String> temp = RecordSetToMap( rs1 );
        String chrq = Util.null2String( temp.get( "ksrq" ) );//日期
        String jsrq = Util.null2String( temp.get( "jsrq" ) );//结束日期

        String sj = temp.get( "kssj" );//时间，以11点为界限区分上下午
        String sxw;//上下午或全天
        if (sj.compareTo( "11:00:00" ) <= 0) {
            sxw = "0";
        } else {
            sxw = "1";
        }

        int rq_rows = 2;
//        out.print(map+"</br>");
        if (map.containsKey( chrq + "_rows" )) {
            if (map.containsKey( chrq + "_" + sxw + "_rows" )) {
                rq_rows = Util.getIntValue( "" + map.get( chrq + "_rows" ) );
            } else {
                rq_rows = Util.getIntValue( "" + map.get( chrq + "_rows" ) ) - 1;
            }
        }
        rq_rows++;
        map.put( chrq + "_rows", rq_rows );


        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        int rq_sxw_rows = 0;
        if (map.containsKey( chrq + "_" + sxw )) {
            list = (List<Map<String, String>>) map.get( chrq + "_" + sxw );
            rq_sxw_rows = Util.getIntValue( "" + map.get( chrq + "_" + sxw + "_rows" ) );
        }
        rq_sxw_rows++;
        list.add( temp );
        map.put( chrq + "_" + sxw + "_rows", rq_sxw_rows );
        map.put( chrq + "_" + sxw, list );
    }
//out.print("<br>"+JSONObject.fromObject(map).toString());


//如果选择时间段
    String sql1 = "select a.* from " + tablename + " a where sfktap=0 and hysy like '%" + hdnr + "%' and ((ksrq>='" + startdate1 + "' and jsrq<='" + enddate1 + "') " +
            "or (ksrq<'" + startdate1 + "' and jsrq>='" + startdate1 + "' and jsrq<='" + enddate1 + "') " +
            "or (jsrq>'" + enddate1 + "' and ksrq>='" + startdate1 + "' and ksrq<='" + enddate1 + "') " +
            "or (ksrq<'" + startdate1 + "' and jsrq>'" + enddate1 + "')) " +
            "order by ksrq,kssj";
//    out.print(sql1);
    rs2.executeSql( sql1 );//选择跨天后的数据
    while (rs2.next()) {
        Map<String, String> temp = RecordSetToMap( rs2 );
        String chrq = Util.null2String( temp.get( "ksrq" ) );//开始日期
        String jsrq = Util.null2String( temp.get( "jsrq" ) );//结束日期
        String sxw = "2";

        String rq_temp = chrq;
        //如果选择时间段，从时间段的开始时间，到结束时间，一直增加对应的数据
        while (rq_temp.compareTo( jsrq ) <= 0) {

            int rq_rows = 2;

            if (map.containsKey( rq_temp + "_rows" )) {
                if (map.containsKey( rq_temp + "_" + sxw )) {//如果已经有全天的数据
                    rq_rows = Util.getIntValue( "" + map.get( rq_temp + "_rows" ) );
                } else {
                    rq_rows = Util.getIntValue( "" + map.get( rq_temp + "_rows" ) ) - 1;
                }
            }
            rq_rows++;
            map.put( rq_temp + "_rows", rq_rows );

            List<Map<String, String>> list = new ArrayList<Map<String, String>>();
            int rq_sxw_rows = 0;
            if (map.containsKey( rq_temp + "_" + sxw )) {
                list = (List<Map<String, String>>) map.get( rq_temp + "_" + sxw );
                rq_sxw_rows = Util.getIntValue( "" + map.get( rq_temp + "_" + sxw + "_rows" ) );
            }
            rq_sxw_rows++;
            list.add( temp );
            map.put( rq_temp + "_" + sxw + "_rows", rq_sxw_rows );
            map.put( rq_temp + "_" + sxw, list );

            rq_temp = sdf.format( dayAdd( sdf.parse( rq_temp ), "1" ) );
        }
    }
%>
<%!
    public String getFirstDayByDate(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
        Date _date = new Date();
        if (!date.equals( "" )) {
            _date = sdf.parse( date );
        }
        calendar.setTime( _date );
        calendar.set( Calendar.WEEK_OF_MONTH, 1 );

        return sdf.format( calendar.getTime() );
    }

    public String getLastDayByDate(String date) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
        Date _date = new Date();
        if (!date.equals( "" )) {
            _date = sdf.parse( date );
        }
        calendar.setTime( _date );
        calendar.add( Calendar.MONTH, 1 );
        calendar.set( Calendar.DATE, 1 );
        calendar.add( Calendar.DATE, -1 );

        return sdf.format( calendar.getTime() );
    }

    public Map<String, String> RecordSetToMap(RecordSet rs) {
        Map<String, String> map = null;
        String[] colNames = rs.getColumnName();
        if (colNames != null && colNames.length > 0) {
            map = new HashMap<String, String>();
            for (int i = 0; i < colNames.length; i++) {
                String fieldname = colNames[i];
                String fieldvalue = rs.getString( fieldname );
                map.put( fieldname.toLowerCase(), fieldvalue );
            }
        }
        return map;
    }

    public Date dayAdd(Date date, String num) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime( date );
            calendar.add( Calendar.DATE, Util.getIntValue( num ) );

            return calendar.getTime();
        } catch (Exception e) {
            return new Date();
        }
    }
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <SCRIPT language="javascript" src="/js/weaver_wev8.js"></script>
    <SCRIPT language="javascript" src="/js/weaverTable_wev8.js"></script>
    <SCRIPT type="text/javascript" src="/formmode/js/WdatePicker/WdatePicker_wev8.js"></script>
    <LINK REL=stylesheet type=text/css HREF=/css/Weaver_wev8.css>
    <link href="/formmode/css/formmode_wev8.css" type="text/css" rel="stylesheet"/>
    <script language=javascript src="/wui/common/jquery/plugin/Listener_wev8.js"></script>
    <SCRIPT type="text/javascript" src="/js/workflow/VCEventHandle_wev8.js"></SCRIPT>
    <script src='echarts.min.js'></script>
    <script src='macarons.js'></script>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            border: 0;
            list-style-type: none;
            font-size: 14px;
        }

        td, th {
            border-bottom: 1px solid gray;
            border-right: 1px solid gray;
            height: 30px;
            text-align: center;
        }

        th {
            font-weight: bold;
        }

        .btn {
            height: 30px;
            width: 1100px;
            border: 1px solid black;
            background-color: white;
        }

        #tablecontainer, #tablecontainer1 {
            width: 100%;
            height: 400px;
            margin: 0 auto;
        }

        a {
            color: #018efb;
        }

        a:hover {
            text-decoration: underline !important;
        }
    </style>
    <script type="text/javascript">
        function onSearchWFQTDate(spanname, inputname) {
            var oncleaingFun = function () {
                $(spanname).innerHTML = '';
                inputname.value = '';
            }
            WdatePicker({
                el: spanname, onpicked: function (dp) {
                    var returnvalue = dp.cal.getDateStr();
                    $dp.$(inputname).value = returnvalue;
                }, oncleared: oncleaingFun
            });
        }

        var temp_sd = "<%=startdate1 %>";
        var temp_ed = "<%=enddate1 %>";
        var temp_ws = 0;

        jQuery(document).ready(function () {
            //temp_ws = getws();
            //temp_ws = getws();
            //var date = new Date();
            //var week = date.getDay();
            //temp_sd = get(date, 1-week);
            //date =new Date(temp_sd);
            //temp_ed = get(date, 7);

            //setIfr(0);
        });

        function getshowlog() {

            $("#dialogfrm").dialog({
                autoOpen: false,   // 是否自动弹出窗口
                modal: true,    // 设置为模态对话框
                resizable: true,
                width: 410,   //弹出框宽度
                height: 240,   //弹出框高度
                title: "用户登录",  //弹出框标题
                position: "center",  //窗口显示的位置
                buttons: {
                    '确定': function () {
//调用登录的方法
                    },
                    '取消': function () {
                       // $(this).dialog("close");
                    }
                }
            });

        }

        function setIfr(type) {
            if (type == 0) {
                temp_ws = getws();
                var date = new Date();
                var week = date.getDay();
                temp_sd = get(date, 1 - week);
                date = new Date(temp_sd);
                temp_ed = get(date, 6);
            } else if (type == -1) {
                temp_ws = temp_ws - 1;
                var date = new Date(temp_sd);
                temp_sd = get(date, -7);
                date = new Date(temp_sd);
                temp_ed = get(date, 6);
            } else if (type == 1) {
                temp_ws = temp_ws + 1;
                var date = new Date(temp_sd);
                temp_sd = get(date, 7);
                date = new Date(temp_sd);
                temp_ed = get(date, 6);
            }

            //alert(temp_ws+"--"+temp_sd+"--"+temp_ws);
            var hdnr = document.getElementById("hdnr").value;
            window.location.href = "bzzdap.jsp?formid=<%=formid%>&type=1&startdate1=" + temp_sd + "&enddate1=" + temp_ed + "&field37274=" + hdnr;
        }

        function getws() {
            var today = new Date();
            var firstDay = new Date(today.getFullYear(), 0, 1);
            var dayOfWeek = firstDay.getDay();
            var spendDay = 1;
            if (dayOfWeek != 0) {
                spendDay = 7 - dayOfWeek + 1;
            }
            firstDay = new Date(today.getFullYear(), 0, 1 + spendDay);
            var d = Math.ceil((today.valueOf() - firstDay.valueOf()) / 86400000);
            var result = Math.ceil(d / 7);
            return result + 1;
        }

        function get(date, n) {
            date.setDate(date.getDate() + n);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            if (month < 10) {
                month = "0" + month;
            }
            if (day < 10) {
                day = "0" + day;
            }
            return year + "-" + month + "-" + day;
        }

        function doExcel() {
            var startdate1 = "<%=startdate1%>";
            var enddate1 = "<%=enddate1%>";
            window.location.href = "bzzdap_excel.jsp?formid=<%=formid%>&startdate1=" + temp_sd + "&enddate1=" + temp_ed;
        }

        function doSearch() {

            var rq = document.getElementById("rq_span").innerText;
            var hdnr = document.getElementById("hdnr").value;

            window.location.href = "bzzdap.jsp?formid=<%=formid%>&type=0&rq=" + rq + "&field37274=" + hdnr;
        }

        function doCreate() {
            //  window.open("/spa/cube/index.html#/main/cube/card?type=1&modeId=8&formId=-31");
            getshowlog();
            //showDialog("/spa/cube/index.html#/main/cube/card?type=1&modeId=8&formId=-31");

        }

        //删除
        function doDelete(id) {
            const xhr = new XMLHttpRequest()
            xhr.open("GET", "/interface/zjs/doDelete.jsp?id=" + id, true)
            xhr.withCredentials = true
            xhr.onreadystatechange = function () {
                if (xhr.readyState != xhr.DONE && xhr.responseText != "") {
                    var flag = xhr.responseText.trim()
                    alert(flag);
                    location.reload();
                }
            }
            xhr.send()
        }

        //修改
        function doUpdate(id) {
            //  window.open("/spa/cube/index.html#/main/cube/card?type=2&modeId=8&formId=-31&billid=" + id);
            showDialog("/spa/cube/index.html#/main/cube/card?type=2&modeId=8&formId=-31&billid=" + id);
        }

        //查看
        function doSelect(id) {
            // window.open("/spa/cube/index.html#/main/cube/card?type=0&modeId=8&formId=-31&billid=" + id);
            showDialog("/spa/cube/index.html#/main/cube/card?type=0&modeId=8&formId=-31&billid=" + id);
        }

        弹出框方法

        function showDialog(url) {
            var diaHeight = document.body.clientHeight * 0.8;
            var diaWidth = document.body.clientWidth * 0.8;
            dialog = new top.Dialog();
            dialog.currentWindow = window;
            dialog.Drag = true;
            dialog.Width = diaWidth;
            dialog.Height = diaHeight;
            dialog.URL = url;
            dialog.show();
        }

        /*function _refresh(){
            window.location.reload();
        }*/

    </script>
</head>

<body>
<table cellpadding="0" cellspacing="0" border="0"
       style="width: 100%;border-top: 1px solid gray;border-left: 1px solid gray;">
    <%
        if (!from.equals( "portal" )) {
    %>
    <tr>
        <th colspan="<%=4 %>" style="text-align: left;padding-left: 20px;border-right: 0;">
            日期：
            <button class="calendar" onclick="onSearchWFQTDate(rq_span,rq)" type="button"></button>
            <input type="hidden" class="calendar" id="rq" name="rq" onclick="WdatePicker()" value="<%=rq %>"/>
            <span id="rq_span" name="rq_span"><%=rq %></span>
            会议事由：<input type="text" id="hdnr" name="hdnr" value="<%=hdnr %>"/>
            <input type="button" value="查询" onclick="doSearch()" class="middle e8_btn_top_first"
                   style="margin-left: 20px;"/>
        </th>
        <th colspan="<%=4 %>" style="text-align: right;padding-left: 20px;">
            <input type="button" value="新建记录" onclick="doCreate()" class="middle e8_btn_top_first"
                   style="margin-right: 100px;"/>
            <input type="button" value="上一周" onclick="setIfr(-1)" class="middle e8_btn_top_first"
                   style="margin-right: 20px;"/>
            <input type="button" value="本周" onclick="setIfr(0)" class="middle e8_btn_top_first"
                   style="margin-right: 20px;"/>
            <input type="button" value="下一周" onclick="setIfr(1)" class="middle e8_btn_top_first"
                   style="margin-right: 20px;"/>
            <input type="button" value="导出" onclick="doExcel()" class="middle e8_btn_top_first"
                   style="margin-right: 100px;"/>
        </th>
    </tr>
    <tr>
        <th colspan="<%=8 %>" style="font-size: 20px;height: 50px;">
            第（<%=weekOfYear %>）周 <%=startdate1 %> ~ <%=enddate1 %>
        </th>
    </tr>
    <%
        }
    %>
    <tr style="background-color: #c4e2f9;">
        <th style="width: 8%;" colspan="2">日期</th>
        <th style="width: 10%;">会议时间</th>
        <th style="width: 10%;">参加人员</th>
        <th style="width: 10%;">地点</th>
        <th style="width: 10%;">会议事由</th>
        <th style="width: 10%;">阅读状态</th>
        <th style="width: 10%;">操作</th>
    </tr>
    <%
        String _rq = startdate1;
        int index = 0;
        while (_rq.compareTo( enddate1 ) <= 0) {
            int rq_index = 0;
            int rq_rows = Util.getIntValue( Util.null2String( map.get( _rq + "_rows" ) ) );

            List<Map<String, String>> list_rq = (List<Map<String, String>>) map.get( _rq );

            Date ksrqDate = sdf.parse( _rq );
            String weekStr = weekArr[ksrqDate.getDay()];
            String __rq = _rq.substring( 5 );
            __rq = __rq.replace( "-", "月" ) + "日";

            if (rq_rows <= 0) {
    %>
    <tr>
        <td rowspan=3><%=__rq + "<br>" + "(" + weekStr + ")" %>
        </td>
        <td>上午</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>下午</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td>全天</td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <%
    } else if (rq_rows > 0) {
        for (int i : sjdIs) {
            int rq_sxw_rows = Util.getIntValue( Util.null2String( map.get( _rq + "_" + i + "_rows" ) ) );
            int rq_sxw_index = 0;
            if (rq_sxw_rows <= 0) {
                rq_index++;
                rq_sxw_index++;
    %>
    <tr>
        <%if (rq_index == 1) { %>
        <td rowspan="<%=rq_rows %>"><%=__rq + "<br>" + "(" + weekStr + ")" %>
        </td>
        <%} %>
        <%if (rq_sxw_index == 1) { %>
        <td><%=sjd[i] %>
        </td>
        <%} %>
        <%if (i == 0) { %>
        <td></td>
        <%} else if (i == 1) {%>
        <td></td>
        <%} else if (i == 2) {%>
        <td></td>
        <%} %>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <%
    } else if (rq_sxw_rows > 0) {
        List<Map<String, String>> list = (List<Map<String, String>>) map.get( _rq + "_" + i );
        for (Map<String, String> temp : list) {
            rq_index++;
            rq_sxw_index++;
            String sfktap = Util.null2String( temp.get( "sfktap" ) );//是否跨天安排37275
            String sj = Util.null2String( temp.get( "kssj" ) );//开始时间37277
            String sj_temp = sj.substring( 0, 5 );
            if (sfktap.equals( "0" )) {
                sj_temp = "全天";
            }
            //訾建设
            String ydzt = Util.null2String( temp.get( "ydzt" ) );//阅读状态
            String hysy = Util.null2String( temp.get( "hysy" ) );//会议是由
            String tzrxm = Util.null2String( temp.get( "tzrxm" ) );//通知人员
            String dd = Util.null2String( temp.get( "dd" ) );//地点
            String kssj = Util.null2String( temp.get( "kssj" ) );//开始时间
            String jssj = Util.null2String( temp.get( "jssj" ) );//结束时间
            String ksrq = Util.null2String( temp.get( "ksrq" ) );//开始时间
            String jsrq = Util.null2String( temp.get( "jsrq" ) );//结束时间
            String formida = Util.null2String( temp.get( "id" ) );//结束时间
            calendar.setTime( sdf.parse( ksrq ) );
            calendar.add( Calendar.DATE, index );
            String format = sdf.format( calendar.getTime() );
            index = index + 1;
    %>
    <tr>
        <%if (rq_index == 1) { %>
        <td rowspan="<%=rq_rows %>"><%=__rq + "<br>" + "(" + weekStr + ")" %>
        </td>
        <%} %>
        <%if (rq_sxw_index == 1) { %>
        <td rowspan="<%=rq_sxw_rows %>"><%=sjd[i] %>
        </td>
        <%} %>
        <%
            if (i == 2) {
                if (format.equals( ksrq )) {
        %>
        <td style="text-align: left;">
            <%=kssj + "开始" %>
        </td>
        <%
        } else if (format.equals( jsrq )) {
        %>
        <td style="text-align: left;">
            <%=kssj + "结束"%>
        </td>
        <%
        } else {
        %>

        <td style="text-align: left;">
            全天
        </td>
        <%
            }
        } else {%>
        <td style="text-align: left;">
            <%=kssj + "~" + jssj %>
        </td>
        <%} %>


        <td><%=getUserName( tzrxm ) %>
        </td>
        <td><%=dd %>
        </td>
        <td><%=hysy %>
        </td>
        <td><%=ydzt %>
        </td>
        <td>
            <input type="button" value="删除" onclick="doDelete(<%=formida%>)"
                   style="margin-right: 100px;"/>
            <input type="button" value="修改" onclick="doUpdate(<%=formida%>)"
                   style="margin-right: 100px;"/>
            <input type="button" value="详情" onclick="doSelect(<%=formida%>)"
                   style="margin-right: 100px;"/>
        </td>
    </tr>
    <%
                        }
                    }
                }
            }
            _rq = sdf.format( dayAdd( sdf.parse( _rq ), "1" ) );
        }
    %>
</table>
<div id="dialogfrm" style="display:none">

    <iframe id="frame" style="border: 0px;" src="/spa/cube/index.html#/main/cube/card?type=0&modeId=8&formId=-31"
            width='100%' height='100%'></iframe>

</div>

</body>
</html>
<style>
    a {
        text-decoration: none;
    }
</style>
<%! public String getUserName(String userid) {
    RecordSet rs = new RecordSet();
    rs.execute( "select * from HrmResource where id in (" + userid + ")" );
    String name = "";
    while (rs.next()) {
        name += Util.null2String( rs.getString( "lastname" ) ) + ",";
    }
    if (name.length() > 0) {
        name = name.substring( 0, name.length() - 1 );
    }
    return name;
}
%>